package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Hiae;
import com.ruoyi.system.domain.Statistics;
import com.ruoyi.system.service.IHiaeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 家庭收支Controller
 * 
 * @author zzx
 * @date 2020-09-16
 */
@RestController
@RequestMapping("/system/hiae")
public class HiaeController extends BaseController
{
    @Autowired
    private IHiaeService hiaeService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询家庭收支列表
     */
    @PreAuthorize("@ss.hasPermi('system:hiae:list')")
    @GetMapping("/list")
    public TableDataInfo list(Hiae hiae) {
        LoginUser  loginUser2= SecurityUtils.getLoginUser();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        List<Hiae> list2 = hiaeService.selectHiaeList(hiae);
        Map<String, Double> collect3 = list2.stream().collect(Collectors.groupingBy(Hiae::getType, Collectors.summingDouble(Hiae::getRemarks)));
        collect3.put("balance",(collect3.get("s")==null?0D:collect3.get("s")) - (collect3.get("z")==null?0D:collect3.get("z")));
        startPage();
        List<Hiae> list = hiaeService.selectHiaeList(hiae);
        return getDataTable(list,collect3);
    }

    /**
     * 导出家庭收支列表
     */
    @PreAuthorize("@ss.hasPermi('system:hiae:export')")
    @Log(title = "家庭收支", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Hiae hiae)
    {
        List<Hiae> list = hiaeService.selectHiaeList(hiae);
        ExcelUtil<Hiae> util = new ExcelUtil<Hiae>(Hiae.class);
        return util.exportExcel(list, "hiae");
    }

    /**
     * 获取家庭收支详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:hiae:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hiaeService.selectHiaeById(id));
    }

    /**
     * 新增家庭收支
     */
    @PreAuthorize("@ss.hasPermi('system:hiae:add')")
    @Log(title = "家庭收支", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Hiae hiae) {
        List<Hiae> s= hiaeService.selectHiaeList(hiae);
        if(s.size()>0){
            return new AjaxResult(999,"请勿新增重复数据");
        }

        return toAjax(hiaeService.insertHiae(hiae));
    }

    /**
     * 修改家庭收支
     */
    @PreAuthorize("@ss.hasPermi('system:hiae:edit')")
    @Log(title = "家庭收支", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Hiae hiae)
    {
        return toAjax(hiaeService.updateHiae(hiae));
    }

    /**
     * 删除家庭收支
     */
    @PreAuthorize("@ss.hasPermi('system:hiae:remove')")
    @Log(title = "家庭收支", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hiaeService.deleteHiaeByIds(ids));
    }
}
